//SPDX-FileCopyrightText: Copyright 2022-2024 深圳市同心圆网络有限公司
//SPDX-License-Identifier: GPL-3.0-only

package event_dao

import (
	"context"

	"atomgit.com/openlinksaas/api-server/dao/db_ns"
	"atomgit.com/openlinksaas/proto-gen-go.git/events_subscribe_api"
	"go.mongodb.org/mongo-driver/bson"
	"go.mongodb.org/mongo-driver/mongo"
	"go.mongodb.org/mongo-driver/mongo/options"
	"google.golang.org/protobuf/proto"
)

type SubscribeInfoDbItem struct {
	SubscribeId     string `bson:"_id"`
	ProjectId       string `bson:"projectId"`
	ChatBotName     string `bson:"chatBotName"`
	ChatBotType     uint32 `bson:"chatBotType"`
	ChatBotAddr     string `bson:"chatBotAddr"`
	ChatBotSignCode string `bson:"chatBotSignCode"`
	EventCfgData    []byte `bson:"eventCfgData"`
	CreateTime      int64  `bson:"createTime"`
	CreateUserId    string `bson:"createUserId"`
	UpdateTime      int64  `bson:"updateTime"`
	UpdateUserId    string `bson:"updateUserId"`
}

func (item *SubscribeInfoDbItem) ToSubscribeInfo() (*events_subscribe_api.SubscribeInfo, error) {
	eventCfg := &events_subscribe_api.EventCfg{}
	err := proto.Unmarshal(item.EventCfgData, eventCfg)
	if err != nil {
		return nil, err
	}
	return &events_subscribe_api.SubscribeInfo{
		SubscribeId:  item.SubscribeId,
		ProjectId:    item.ProjectId,
		ChatBotName:  item.ChatBotName,
		EventCfg:     eventCfg,
		CreateTime:   item.CreateTime,
		CreateUserId: item.CreateUserId,
		UpdateTime:   item.UpdateTime,
		UpdateUserId: item.UpdateUserId,
	}, nil
}

type _SubscribeDao struct {
	mc *mongo.Collection
}

func newSubscribeDao(db *mongo.Database) (*_SubscribeDao, error) {
	mc := db.Collection(db_ns.DB_NS_EVENT + "subscribe")
	_, err := mc.Indexes().CreateOne(context.Background(), mongo.IndexModel{
		Keys: bson.D{{"projectId", 1}},
	})
	if err != nil {
		return nil, err
	}
	return &_SubscribeDao{mc: mc}, nil
}

func (db *_SubscribeDao) Insert(ctx context.Context, dbItem *SubscribeInfoDbItem) error {
	_, err := db.mc.InsertOne(ctx, dbItem)
	return err
}

func (db *_SubscribeDao) Update(ctx context.Context, projectId, subscribeId, chatBotName string, eventCfgData []byte, updateTime int64, updateUserId string) error {
	_, err := db.mc.UpdateOne(ctx, bson.M{"_id": subscribeId, "projectId": projectId},
		bson.M{"$set": bson.M{
			"chatBotName":  chatBotName,
			"eventCfgData": eventCfgData,
			"updateTime":   updateTime,
			"updateUserId": updateUserId,
		}})
	return err
}

func (db *_SubscribeDao) List(ctx context.Context, projectId string) ([]*SubscribeInfoDbItem, error) {
	cursor, err := db.mc.Find(ctx, bson.M{"projectId": projectId}, options.Find().SetSort(bson.M{"createTime": -1}))
	if err != nil {
		return nil, err
	}
	retList := []*SubscribeInfoDbItem{}
	err = cursor.All(ctx, &retList)
	return retList, err
}

func (db *_SubscribeDao) Exist(ctx context.Context, projectId, subscribeId string) (bool, error) {
	count, err := db.mc.CountDocuments(ctx, bson.M{"_id": subscribeId, "projectId": projectId})
	return count > 0, err
}

func (db *_SubscribeDao) Get(ctx context.Context, projectId, subscribeId string) (*SubscribeInfoDbItem, error) {
	retItem := &SubscribeInfoDbItem{}
	err := db.mc.FindOne(ctx, bson.M{"_id": subscribeId, "projectId": projectId}).Decode(retItem)
	return retItem, err
}

func (db *_SubscribeDao) Remove(ctx context.Context, projectId, subscribeId string) error {
	_, err := db.mc.DeleteOne(ctx, bson.M{"_id": subscribeId, "projectId": projectId})
	return err
}
